/**
 * Created by LeungZ on 2016/10/17.
 */
import serviceBaseDrct from './ser-comp/ser-base/base.drct.js';
import serviceAdvDrct from './ser-comp/ser-adv/advance.drct.js';
import serviceVersionTable from './ser-comp/ser-table/table.drct.js';

import temp from './service.html';
import ctrl from './service.ctrl.js';

import dtlTemp from './detail/detail.html';
import dtlCtrl from './detail/detail.ctrl.js';

import './create/create.scss';
import creTemp from './create/create.html';
import creCtrl from './create/create.ctrl.js';

import creBaseTemp from './create/base/base.html';
import creBaseCtrl from './create/base/base.ctrl.js';

import creDeployTemp from './create/deployment/deployment.html';
import creDeployCtrl from './create/deployment/deployment.ctrl.js';

import creReleaseTemp from './create/release/release.html';
import creReleaseCtrl from './create/release/release.ctrl.js';

import creVerTemp from './detail/create/create.html';
import creVerCtrl from './detail/create/create.ctrl.js';

import creVerDeployTemp from './detail/create/deployment/deployment.html';
import creVerDeployCtrl from './detail/create/deployment/deployment.ctrl.js';


export default angular.module('ctg-k8sconsole')
    .directive('serviceConfigBase', serviceBaseDrct.factory)
    .directive('serviceConfigAdvance', serviceAdvDrct.factory)
    .directive('serviceVersionTable', serviceVersionTable.factory)
    .config(['$stateProvider', $stateProvider=> {
        $stateProvider.state('app-service', {
            url: '/app/service',
            templateUrl: temp,
            controller: ctrl,
            controllerAs: 'list',
            resolve: {
                list: ['appSrv', 'ctgNotification', function (appSrv, ctgNotification) {
                    return appSrv.getAppList({currentPage: 1}).catch(function (data) {
                        ctgNotification.error('获取应用列表数据有误.');
                    });
                }]
            }
        }).state('app-service-publish', {
            templateUrl: creTemp,
            controller: creCtrl,
            controllerAs: 'vm',
            abstract: true,
            resolve: {
                appImgList: ['appSrv', 'ctgNotification', '$stateParams', function (appSrv, ctgNotification) {
                    return appSrv.getBuildImages().catch(function (data) {
                        ctgNotification.error('获取应用镜像列表数据有误.');
                    });
                }],
                clusterList: ['clusterSrv', 'ctgNotification', '$stateParams', function (clusterSrv, ctgNotification) {
                    return clusterSrv.getClusterList({perPage: 99, currentPage: 1}).catch(function (data) {
                        ctgNotification.error('获取容器集群列表数据有误.');
                    });
                }]
            }
        }).state('app-service-publish.base', {
            url: "/app/service/publish/base",
            templateUrl: creBaseTemp,
            controller: creBaseCtrl,
            controllerAs: 'vm'
        }).state('app-service-publish.deploy', {
            url: '/app/service/publish/deploy',
            templateUrl: creDeployTemp,
            controller: creDeployCtrl,
            controllerAs: 'vm'
        }).state('app-service-publish.release', {
            url: '/app/service/publish/release',
            templateUrl: creReleaseTemp,
            controller: creReleaseCtrl,
            controllerAs: 'vm'
        }).state('app-service-detail', {
            url: '/app/service/:id',
            templateUrl: dtlTemp,
            controller: dtlCtrl,
            controllerAs: 'dtl',
            resolve: {
                detail: ['appSrv', 'ctgNotification', '$stateParams', function (appSrv, ctgNotification, $stateParams) {
                    return appSrv.getAppDetail($stateParams.id).catch(function (data) {
                        ctgNotification.error('获取应用详情数据有误.');
                    });
                }],
                services: ['appSrv', 'ctgNotification', '$stateParams', function (appSrv, ctgNotification, $stateParams) {
                    return appSrv.getServiceOfApp({id: $stateParams.id, currentPage: 1}).catch(function (data) {
                        ctgNotification.error('获取应用服务列表数据有误.');
                    });
                }]
            }
        }).state('app-service-version-publish', {
            url: '?k8sCode&appId&serId&verId&rolVerId&namespace',
            templateUrl: creVerTemp,
            controller: creVerCtrl,
            controllerAs: 'vm',
            abstract: true,
            resolve: {
                appImgList: ['appSrv', 'ctgNotification','$stateParams', function (appSrv, ctgNotification, $stateParams) {
                    if(!$stateParams.verId) {
                        return appSrv.getBuildImages().catch(function (data) {
                            ctgNotification.error('获取应用镜像列表数据有误.');
                        });
                    }
                }],
                version:['appSrv', 'ctgNotification', '$stateParams', function (appSrv, ctgNotification, $stateParams) {
                    if($stateParams.verId){
                        return appSrv.getVerDetail($stateParams.verId).catch(function (data) {
                            ctgNotification.error('获取服务版本数据有误.');
                        });
                    }
                }],
                service:['appSrv', 'ctgNotification', '$stateParams', function (appSrv, ctgNotification, $stateParams) {
                    if($stateParams.serId){
                        return appSrv.getServiceDetail($stateParams.serId).catch(function (data) {
                            ctgNotification.error('获取服务数据有误.');
                        });
                    }
                }]
            }
        }).state('app-service-version-publish.deploy', {
            url: '/app/service/version/publish/deploy',
            templateUrl: creVerDeployTemp,
            controller: creVerDeployCtrl,
            controllerAs: 'service'
        }).state('app-service-version-publish.release', {
            url: '/app/service/version/publish/release',
            templateUrl: creReleaseTemp,
            controller: creReleaseCtrl,
            controllerAs: 'vm'
        }).state('app-service-version-history', {
            url: '/app/service/:appId/:serId/history',
            templateUrl: dtlTemp,
            controller: dtlCtrl,
            controllerAs: 'vm',
            resolve: {

            }
        })
    }]);